package com.huawei.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Code3 {
    public static void main(String[] args) {
        Code3 code3 = new Code3();
        code3.lengthOfLongestSubstring(" ");
    }

    public int lengthOfLongestSubstring(String s) {
        if (s.length()==0) return 0;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int max = 0;
        int left = 0;
        for(int i = 0; i < s.length(); i ++){
            if(map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
    }

    public int lengthOfLongestSubstring1(String s) {
        int max_length = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i),i);
            int j = i + 1;
            while (j < s.length()) {
                if(map.containsKey(s.charAt(j))){
                    break;
                }else {
                    map.put(s.charAt(j),j);
                }
                j++;
            }
            max_length = Math.max(j-i,max_length);
            map.clear();
        }
        return max_length;
    }
}
